import * as types from "../../mutation-types";
import api from "../../../api";
import { home_path } from "../../../utils/system/system";

// * setMenuList
export const setMenuList = (menuList) => ({
	type: types.SET_MENU_LIST,
	data: menuList
});

// * set current page title
export const setCurrentPageTitleAction = (title) => ({
	type: types.SET_PAGE_TITLE,
	data: title
});

// 以下测试异步

// * redux-thunk
export const getMenuListActionThunk = () => {
	return async (dispatch) => {
		const res = await api.auth.getMenuList();
		dispatch({
			type: types.SET_MENU_LIST,
			data: (res.data) ?? []
		});
	};
};

// * set current page title
export const setPageTitleActionThunk = (title) => {
  return (dispatch) => {
    setTimeout(() => {
      dispatch({
        type: types.SET_PAGE_TITLE,
        data: title
      });
    }, 0)
	};
};

// * redux-promise《async/await》
export const getMenuListAwaitAction = async () => {
	const res = await api.auth.getMenuList();
	return {
		type: types.SET_MENU_LIST,
		data: res && res.data ? [home_path].concat(res.data) : [home_path]
	};
};


// * redux-promise《async/await》
export const getMenuListActionPromise = async () => {
	return await (async (dispatch) => {
		const res = await api.auth.getMenuList().catch(err => {
      console.log(err)
    });
		dispatch({
			type: types.SET_MENU_LIST,
			data: (res?.data) ?? []
		});
	});
};